
package CtrlLayer;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

/**
 * @version 12/22/2011
 * @author Gruppe 4
 */

public class CtrlOrder 
{
    private Order order;
    private Customer customer;
    private CtrlPerson ctrlPerson;
    private CtrlTask ctrlTask;
    private CtrlMachine ctrlMachine;
    private CtrlProduct ctrlProduct;
    private PartOrder po;
    private IFDBOrder dbOrder;
    
    /**
     * Creates a new instance of CtrlOrder
     */
    public CtrlOrder()
    {
        order = new Order();
        ctrlPerson = new CtrlPerson();
        ctrlTask = new CtrlTask();
        ctrlMachine = new CtrlMachine();
        ctrlProduct = new CtrlProduct();
        po = new PartOrder();
        dbOrder = new DBOrder();
    }
    
    /**
     * Method to get an ArrayList with orders from a specific customer, used in ListModelSearchPersonOrder
     * @param customerId int customerId the id of the Customer
     * @param retrieveAssociation 
     * @return returns an ArrayList with orders for a specific customer
     */
    public ArrayList<Order> findOrders(int customerId, boolean retrieveAssociation)
    {
        return dbOrder.findOrders(customerId, retrieveAssociation);
    }
    
    /**
     * Method to get an ArrayList with order with a specific orderId
     * @param orderId int orderId the id of the order
     * @param retrieveAssociation
     * @return returns an ArrayList with orders
     */
    public ArrayList<Order> findOrdersByOrderId(int orderId, boolean retrieveAssociation)
    {
        return dbOrder.findOrdersByOrderId(orderId, retrieveAssociation);
    }
    
    /**
     * Method that finds an Order with a specific orderID
     * @param orderId int orderId the id of the order 
     * @param retrieveAssociation
     * @return returns an Order object
     */
    public Order findOrderById(int orderId, boolean retrieveAssociation)
    {
        return dbOrder.searchOrderId(orderId, retrieveAssociation);
    }
    /**
     * Method that finds an Order with a specific date
     * @param orderDate String orderDate the date the order has been made
     * @return returns an ArrayList with orders made on a specific date
     */
    public ArrayList<Order> findOrderByDate(String orderDate)
    {
        return dbOrder.searchOrderDate(orderDate, false);
    }
    
    /**
     * 
     * @param orderDate
     * @param tasks
     * @param tenderDate
     * @param tenderStatus
     * @param totalPrice
     * @param customer
     * @param invoice
     * @return 
     */
    public int updateOrder(String orderDate, Task tasks, String tenderDate,int tenderStatus,
                           double totalPrice, Customer customer, Invoice invoice)
    {
        IFDBOrder dbOrder = new DBOrder();
        Order order = new Order();
        order.setOrderDate(orderDate);
        order.setTenderDate(tenderDate);
        order.setTenderStatus(tenderStatus);
        order.setTotalPrice(totalPrice);
        order.setCustomer(customer);
        order.setTasks(tasks);
        order.setInvoice(invoice);
        return dbOrder.updateOrder(order);  
    }
    
    /**
     * Method that deletes an order from the database
     * @param orderId int orderId the id of the order 
     */
    public void deleteOrder(int orderId )
    {
        dbOrder.deleteOrder(orderId);
    }
    
    public void startSale(Customer customer, int employeeId, String currentDate,
             String taskName, String disclaimer, String startDate, String endDate)
    { 
        order.setTenderDate(currentDate);
        order.setTenderStatus(1);
        order.setCustomer(customer);
        order.setEmployee(ctrlPerson.findPersonById(employeeId, true));
        order.setInvoice(null);
        
        order.setTasks(setTask(taskName, disclaimer, startDate, endDate));
    }
    
    public PartOrder addEmployee(int employeeId, double amount)
    {
        Person p = ctrlPerson.findPersonById(employeeId, true);
        System.out.println(p);
        
        po = setPartOrder(p, amount);
        order.getTasks(0).setPartOrders(po);
        
        return po;
    }
    
    public PartOrder addMachine(int machineId, double amount)
    {
        Machine m = ctrlMachine.findMachine(machineId, false);
        
        po = setPartOrderMachine(m, amount);
        order.getTasks(0).setPartOrders(po);
        return po;
    }
    
    public PartOrder addProduct(int productId, double amount)
    {
        ProductDescription pd = ctrlProduct.findProduct(productId, false);
        System.out.println(pd);
        
        
        po = setPartOrderProduct(pd, amount);
        order.getTasks(0).setPartOrders(po);
        
        return po;
        
    }
    
    public Task setTask(String taskName, String disclaimer, String startDate, String endDate)//tilføj taskdesc para
    {
        return ctrlTask.createTask(taskName, disclaimer, startDate, endDate);
    }
    
    public PartOrder setPartOrder(int employeeId, double amount)
    {
        return ctrlTask.addEmployee(employeeId, amount);
    }
    
    private PartOrder setPartOrder(Person person, double amount)
    {
        Employee employee = (Employee) person;
        PartOrder partOrder = new PartOrder();
        
        partOrder.setEmployee(employee);
        partOrder.setAmount(amount);
        
        System.out.println(partOrder);
        return partOrder;
    }
    
    private PartOrder setPartOrderMachine(Machine m, double amount)
    {
        PartOrder partOrder = new PartOrder();
        
        partOrder.setMachine(m);
        partOrder.setAmount(amount);
        
        System.out.println(partOrder);
        return partOrder;
    }
    
    private PartOrder setPartOrderProduct(ProductDescription pd, double amount)
    {
        PartOrder partOrder = new PartOrder();
        
        partOrder.setProDesc(pd);
        partOrder.setAmount(amount);
        
        System.out.println(partOrder);
        return partOrder;
    }
    
    public void setTotal(double totalPrice)
    {
        order.setTotalPrice(totalPrice);
    }
    
    public void endSale()
    {
        
        DBConnection con = DBConnection.getInstance();
           try
           {
                int orderId = dbOrder.getMaxId();
                DBConnection.startTransaction();
                dbOrder.insertOrder(order);
                
                for(int i = 0 ; i < order.getSize(); i++)
                {
                    Task task = order.getTasks(i);
                    ctrlTask.insertNewTask(task, orderId);
                    
                    for(int j = 0; j < task.getSize(); j++)
                    {
                        PartOrder partOrder = task.getPartOrders(j);
                        ctrlTask.insertNewPartOrder(partOrder);
                    }
                }
                DBConnection.commitTransaction();
           }
           catch(Exception e)
           {
                DBConnection.rollbackTransaction();
                System.out.println("Transaction has been rolled back");
                System.out.println(e.getMessage());
           }
    }
    
}
